﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework;
using OLTS.DAL;
using OLTS.MODEL;

namespace OLTS.DATA_SERVICE
{
    public class MARITAL_STATUS_Service
    {
        private OTS_MARITAL_STATUS _objApp = new OTS_MARITAL_STATUS();

        public MARITAL_STATUS_Service(OTS_MARITAL_STATUS objApp)
        { 
            _objApp = objApp;
        }

        public MARITAL_STATUS_Service()
        {

        }

        public List<OTS_MARITAL_STATUS> GetAll()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_MARITAL_STATUS.ToList();
                return list;
            }
        }

        public List<OTS_MARITAL_STATUS> GetByCurentLogin(string currentuser)
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_MARITAL_STATUS.Where(x => x.EMPLOYEE_ID == currentuser).ToList();
                return list;
            }
        }

        public void GetAllinclude()
        {

        }

        public void Add(List<OTS_MARITAL_STATUS> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Create)
                {
                    var model = new OTS_MARITAL_STATUS();
                    model.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                    model.SPOUSE_NAME = obj.SPOUSE_NAME;
                    model.OCCUPATION = obj.OCCUPATION;
                    model.NUMBEROFCHILD = obj.NUMBEROFCHILD;
                    model.CREATED = DateTime.Now;
                    
                    using (var ctx = new Entities())
                    {
                        ctx.OTS_MARITAL_STATUS.AddObject(model);
                        ctx.SaveChanges();
                    }
                }
            }
        }

        public void Edit(List<OTS_MARITAL_STATUS> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Update)
                {
                    using (var ctx = new Entities())
                    {
                        var objEdit = ctx.OTS_MARITAL_STATUS.Where(x => x.ID == obj.ID).FirstOrDefault();
                        if (objEdit != null)
                        {
                            objEdit.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                            objEdit.SPOUSE_NAME = obj.SPOUSE_NAME;
                            objEdit.OCCUPATION = obj.OCCUPATION;
                            objEdit.NUMBEROFCHILD = obj.NUMBEROFCHILD;
                            objEdit.CREATED = DateTime.Now;
                            ctx.SaveChanges();
                        }
                    }
                }
            }
        }
         
        public void Delete()
        { 
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_MARITAL_STATUS.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (obj != null)
                {
                    ctx.OTS_MARITAL_STATUS.DeleteObject(obj);
                    ctx.SaveChanges();
                }
                
            }
            
        }

    }
}
